<?php

namespace App\Models\ChatGroup;

use App\Models\BaseModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class ChatGroupUserModel extends BaseModel
{
    use KfSoftDeletes;

    protected $fillable = [
        'shopId',
        'groupId',
        'userId',
        'role',
        'alias',
        'avatar',
    ];

    protected $table = 'chat_group_users';

    protected $appends = [
        'tagText'
    ];

    const ROLE_KF       = 1; // 总部
    const ROLE_BOSS     = 2; // 老板
    const ROLE_EMPLOYEE = 3; // 员工

    const ROLE_MAPPING = [
        self::ROLE_KF       => '总部',
        self::ROLE_BOSS     => '老板',
        self::ROLE_EMPLOYEE => '员工',
    ];

    /**
     * 给 WHOBOT 的映射字段, 不能修改
     */
    const ROLE_FOR_WHOBOT_MAPPING = [
        self::ROLE_KF       => '人工客服',
        self::ROLE_BOSS     => '老板',
        self::ROLE_EMPLOYEE => '店员',
    ];

    const IS_EXIST_ON  = 1;  // 在成员中
    const IS_EXIST_OFF = 0;  // 不在成员中

    const KUA_XIAO_FU_USER_ID = 1; // 夸小父userId

    /*
     * tag 用户标识枚举
     */
    const TAG_DEFAULT          = 0;
    const TAG_ASSISTANT        = 1;
    const TAG_CUSTOMER_SERVICE = 2;
    const TAG_CASHIER_CONSULTANT = 3;

    const TAG_MAPPING = [
        self::TAG_DEFAULT            => '',
        self::TAG_ASSISTANT          => '小助手',
        self::TAG_CUSTOMER_SERVICE   => '在线客服',
        self::TAG_CASHIER_CONSULTANT => '收银顾问',
    ];

    /**
     * tag 访问器
     * @return string
     */
    public function getTagTextAttribute(): string
    {
        return self::TAG_MAPPING[$this->getAttribute('tag')] ?? '';
    }

    /**
     * 查询app端用户是否在群里
     * @param int $shopId
     * @param int $userId
     * @param int $type
     * @return Builder|Model|object|null
     */
    public static function checkAppUserIn(int $shopId, int $userId, int $type)
    {
        $group = ChatGroupModel::query()->where('shopId', $shopId)->where('type', $type)->first();
        if (!$group) {
            return null;
        }

        $ct = (bool)ChatGroupUserModel::query()
                                      ->where('groupId', $group->id)
                                      ->where('userId', $userId)
                                      ->whereIn('role', [self::ROLE_BOSS, self::ROLE_EMPLOYEE])
                                      ->count();

        if (!$ct) {
            return null;
        }

        return $group;
    }

    /**
     * 是否在群组中
     * @param int $groupId
     * @param int $userId
     * @return bool
     */
    public static function inGroup(int $groupId, int $userId): bool
    {
        return (bool)self::query()->where('groupId', $groupId)->where('userId', $userId)->count();
    }

    /**
     * 群聊用户
     * @param int $groupId
     * @param int $role
     */
    public static function getGroupUsers(int $groupId, int $role = 0)
    {
        return Cache::store('array')
                    ->remember("getGroupUsers:{$groupId}:{$role}", 120, function () use ($groupId, $role) {

                        return ChatGroupUserModel::query()
                                                 ->where('groupId', $groupId)
                                                 ->when($role, function ($query) use ($role) {
                                                     $query->where('role', $role);
                                                 })
                                                 ->get();
                    });
    }

    /*
     * 获取群组内的用户列表
     *
     * @param int $groupId
     * @return array
     */
    public static function getUserList(int $groupId): array
    {
        return self::query()
                   ->where('groupId', $groupId)
                   ->select('userId')
                   ->get()
                   ->toArray();
    }
}
